package cn.dnui_yxk801.dnuifood_16110100801.controller;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import cn.dnui_yxk801.dnuifood_16110100801.MainActivity;
import cn.dnui_yxk801.dnuifood_16110100801.R;
import cn.dnui_yxk801.dnuifood_16110100801.bean.ResultBean;
import cn.dnui_yxk801.dnuifood_16110100801.bean.UserBean;
import cn.dnui_yxk801.dnuifood_16110100801.listener.OnRetrofitListener;
import cn.dnui_yxk801.dnuifood_16110100801.model.UserModel;

import static cn.dnui_yxk801.dnuifood_16110100801.MainActivity.userid;

public class UpdateUserActivity extends BaseActivity implements OnRetrofitListener, View.OnClickListener{
    private EditText Uu_et_uname,Uu_et_password,Uu_et_mobilenum,Uu_et_address;
    private Button Uu_btn_update;

    @Override
    void initViews() {
        layout_file=R.layout.activity_update_user;
        setLayout(layout_file);
        Uu_et_uname=findViewById(R.id.Uu_et_uname);
        Uu_et_password=findViewById(R.id.Uu_et_password);
        Uu_et_mobilenum=findViewById(R.id.Uu_et_mobilenum);
        Uu_et_address=findViewById(R.id.Uu_iv_address);
        Uu_btn_update=findViewById(R.id.Uu_btn_update);
    }

    @Override
    void initEvents() {
        Uu_btn_update.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        String uname,password,mobilenum,address;
        uname=Uu_et_uname.getText().toString();
        password=Uu_et_password.getText().toString();
        mobilenum=Uu_et_mobilenum.getText().toString();
        address=Uu_et_address.getText().toString();
        UserModel userModel=new UserModel();
        userModel.updateUserById(userid,uname,password,mobilenum,address,this);
    }

    @Override
    void initDatas() {
        UserModel userModel=new UserModel();
        userModel.getUserById(userid,this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onSuccess(Object object, int flag) {
        if (flag==8){
            ResultBean resultBean=(ResultBean)object;
            if (resultBean.getSuccess().equals("1"))
                Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(this,"修改失败",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(this, MainActivity.class);
            intent.putExtra("userid",userid);
            startActivity(intent);
        }
        if (flag==9){
            UserBean userBean=(UserBean)object;
            Uu_et_uname.setText(userBean.getUsername());
            //Uu_et_password.setText(userBean.getUserpass());
            Uu_et_mobilenum.setText(userBean.getMobilenum());
            Uu_et_address.setText(userBean.getAddress());
        }
    }

    @Override
    public void onFalure(String msg) {

    }


}
